/*
 * 文 件 名:  I18NResource.java
 * 版    权:  Huawei Technologies Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  admin
 * 修改时间:  2013-6-30
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package org.gfz.frame.core.resource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.gfz.frame.core.resource.ResourceInitLoad.ResourceType;
import org.gfz.frame.core.resource.model.I18NResourceModel;
import org.gfz.frame.core.resource.model.ResourceModel;

/**
 * <一句话功能简述> <功能详细描述>
 * 
 * @author 姓名 工号
 * @version [版本号, 2013-6-30]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class I18NResource
{
    public static enum I18NType
    {
        ZH_CN, EN_US
    }
    
    /**
     * 
     */
    private I18NType i18nType;
    
    /**
     * 
     */
    private static I18NResource instance;
    
    /**
     * 
     */
    private Map<String, ResourceBundle> bundleMap;
    
    /**
     * <默认构造函数>
     */
    private I18NResource()
    {
        
    }
    
    /**
     * <一句话功能简述>
     * <功能详细描述>
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static synchronized I18NResource getInstance()
    {
        if (null == instance)
        {
            instance = new I18NResource();
        }
        
        return instance;
    }
    
    /**
     * <一句话功能简述>
     * <功能详细描述>
     * @param key
     * @return
     * @see [类、类#方法、类#成员]
     */
    public String getMessage(String key)
    {
        String message = "";
        
        if (null == key)
        {
            return message;
        }
        
        if (null == bundleMap)
        {
            loadI18NResource();
        }
        
        ResourceBundle bundle = null;
        for (String id : bundleMap.keySet())
        {
            bundle = bundleMap.get(id);
            
            try
            {
                message = bundle.getString(key);
            }
            catch (MissingResourceException e)
            {
                message = "";
            }
            
            if (null != message && !"".equals(message))
            {
                break;
            }
        }
        
        return message;
    }
    
    /**
     * <一句话功能简述>
     * <功能详细描述>
     * @param id
     * @param key
     * @return
     * @see [类、类#方法、类#成员]
     */
    public String getMessage(String id, String key)
    {
        String message = "";
        
        if (null == id || "".equals(id.trim()))
        {
            return getMessage(key);
        }
        
        if (null == key)
        {
            return message;
        }
        
        if (null == bundleMap)
        {
            loadI18NResource();
        }
        
        ResourceBundle bundle = bundleMap.get(id);
        
        if (null != bundle)
        {
            try
            {
                message = bundle.getString(key);
            }
            catch (MissingResourceException e)
            {
                message = "";
            }
        }
        
        return message;
    }
    
    /**
     * <一句话功能简述>
     * <功能详细描述>
     * @param i18nType
     * @see [类、类#方法、类#成员]
     */
    public void setI18N(I18NType i18nType)
    {
        this.i18nType = i18nType;
        
        loadI18NResource();
    }
    
    /**
     * <一句话功能简述>
     * <功能详细描述>
     * @see [类、类#方法、类#成员]
     */
    private void loadI18NResource()
    {
        if (null == bundleMap)
        {
            bundleMap = new HashMap<String, ResourceBundle>();
        }
        
        bundleMap.clear();
        
        if (null == i18nType)
        {
            i18nType = I18NType.ZH_CN;
        }
        
        List<ResourceModel> resList = ResourceInitLoad.getResource(ResourceType.I18N);
        
        if (null == resList)
        {
            return;
        }
        
        I18NResourceModel model = null;
        
        for (ResourceModel temp : resList)
        {
            if (temp instanceof I18NResourceModel)
            {
                model = (I18NResourceModel)temp;
                
                if (model.getI18nType() == i18nType)
                {
                    loadResourceBundle(model);
                }
            }
        }
    }
    
    /**
     * <一句话功能简述> <功能详细描述>
     * 
     * @param model
     * @see [类、类#方法、类#成员]
     */
    private void loadResourceBundle(I18NResourceModel model)
    {
        String id = model.getResId();
        String path = model.getResPath();
        
        File file = new File(path);
        
        if (!file.exists() || !file.isFile())
        {
            return;
        }
        
        ResourceBundle bundle = null;
        FileInputStream in = null;
        
        try
        {
            in = new FileInputStream(file);
            bundle = new PropertyResourceBundle(in);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != in)
            {
                try
                {
                    in.close();
                }
                catch (IOException e)
                {
                    in = null;
                }
            }
        }
        
        if (null != bundle)
        {
            bundleMap.put(id, bundle);
        }
    }
}
